package com.shenzhen.horselive.ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.shenzhen.horselive.R;
import com.shenzhen.horselive.User;
import com.shenzhen.horselive.api.ApiFactory;
import com.shenzhen.horselive.base.AppBarActivity;
import com.shenzhen.horselive.beans.SumBean;
import com.shenzhen.horselive.beans.UserAcountBean;
import com.shenzhen.horselive.databinding.ActivityMyWalletBinding;
import com.shenzhen.horselive.network.SchedulersCompat;
import com.shenzhen.horselive.presenter.LoadingViewObserver;
import com.shenzhen.horselive.presenter.ViewObserver;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class MyWalletActivity extends AppBarActivity {

    ActivityMyWalletBinding  binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMyWalletBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setStyleRed();
        setTitle("我的钱包");
        initView();
        setMenuText("钱包明细");
    }

    @Override
    protected void onResume() {
        super.onResume();
        initdata();
    }

    @Override
    protected void onActionMenuViewClick() {
        super.onActionMenuViewClick();
        //钱包明细
        FenZhiDetailsActivity.start(this,0);
    }

    private void initView() {

     binding.btGo.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             startActivity(new Intent(MyWalletActivity.this,CashOutActivity.class));
         }
     });
    }

    private void initdata() {
        //获取钱包账户
        getAcountForType(0);
        
        getAcountSumForTypeToday(0);
        
        getAcountSumForTypeYesToday(0);

    }

    /**
     * 保留两位小数
     * @param value
     * @return
     */
    private String  keepTwoWei(String  value)
    {
        DecimalFormat format = new DecimalFormat("0.00");
        return format.format(Double.parseDouble(value));
    }


    /**
     * 获取收益今天
     * @param i
     */
    //帐户类型: 0:人民币帐户 ,1:蜂值帐户 ,2:红包股帐户 ,3:金卡帐户
    private void getAcountSumForTypeToday(int i) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        String  staredtime =  sf.format(c.getTime());
        c.add(Calendar.DAY_OF_MONTH, 1);
        String  endtime =  sf.format(c.getTime());
        ApiFactory.getApiService().accountTypesSum(User.get().getMyInfo().getToken()
                ,i+"","1",staredtime,endtime)
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new ViewObserver<SumBean>(this) {
                    @Override
                    protected void onNextInActive(SumBean sumBean) {
                        if (sumBean.isSuccess())
                        {
                          binding.tvTodayProfit.setText("¥"+keepTwoWei(sumBean.getData().getSumValue()));
                        }
                    }
                });

    }
    /**
     * 获取收益昨天
     * @param i
     */
    //帐户类型: 0:人民币帐户 ,1:蜂值帐户 ,2:红包股帐户 ,3:金卡帐户
    private void getAcountSumForTypeYesToday(int i) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        String  endtime =  sf.format(c.getTime());
        c.add(Calendar.DAY_OF_MONTH, -1);
        String  staredtime =  sf.format(c.getTime());
        ApiFactory.getApiService().accountTypesSum(User.get().getMyInfo().getToken()
                ,i+"","1",staredtime,endtime)
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new ViewObserver<SumBean>(this) {
                    @Override
                    protected void onNextInActive(SumBean sumBean) {
                        if (sumBean.isSuccess())
                        {
                            binding.tvYestodayProfit.setText("¥"+keepTwoWei(sumBean.getData().getSumValue()));
                        }
                    }
                });

    }


    //帐户类型: 0:人民币帐户 ,1:蜂值帐户 ,2:红包股帐户 ,3:金卡帐户
    private void getAcountForType(int i) {
        ApiFactory.getApiService().accountTypes(User.get().getMyInfo().getToken(),i+"")
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new LoadingViewObserver<UserAcountBean>(this) {
                    @Override
                    protected void onNextInActive(UserAcountBean userAcountBean) {
                     if (userAcountBean.isSuccess())
                     {
                         binding.tvValue.setText("¥"+keepTwoWei(userAcountBean.getData().getValue()));
                         binding.tvValueDon.setText("¥"+keepTwoWei(userAcountBean.getData().getFreezeAmount()));

                     }
                    }
                });

    }

}